

#include <stdint.h>

#include <Debug.h>
#include "ipcMessage.h"
#include <Reader/Reader.h>
#include "Log.h"

#include <BLE/BLE.h>


int hid_ble_tranceive(uint8_t *cmd, uint16_t clen, uint8_t* resp, uint16_t *rlen, uint32_t timeout){
	BLE_Send_Frame(BLE_Cmd_HID_Transceive, cmd, clen);
	uint8_t code = 0;
	int ret = BLE_Receive_Frame(&code, resp, rlen, timeout);
	if((code & 0x7F) != 0){
		return -2;
	}
	return ret;
}

int hid_ble_get_state(uint8_t states[3], uint32_t timeout){
	BLE_Send_Frame(BLE_Cmd_HID_GetState, NULL, 0);
	uint8_t code = 0;
	uint8_t resp[4];
	uint16_t rlen = 0;
	int ret = BLE_Receive_Frame(&code, resp, &rlen, timeout);
	if(!ret){
		if(code != 0){
			return -2;
		}
		for(int i = 0; i < rlen && i < 3; i++){
			states[i] = resp[i];
		}
	}
	return ret;
}

int hid_ble_eot(uint8_t status, uint32_t timeout){
	BLE_Send_Frame(BLE_Cmd_HID_EOT, &status, 1);
	uint8_t code = 0;
	uint8_t resp[4];
	uint16_t rlen = 0;
	int ret = BLE_Receive_Frame(&code, resp, &rlen, timeout);
	if(code != 0){
		return -2;
	}
	return ret;
}

int hid_ble_set_mode(uint8_t mode, uint32_t timeout){
	BLE_Send_Frame(BLE_Cmd_HID_SetMode, &mode, 1);
	uint8_t code = 0;
	uint8_t resp[4];
	uint16_t rlen = 0;
	int ret = BLE_Receive_Frame(&code, resp, &rlen, timeout);
	return ret;
}

